package org.diffshop.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.diffshop.common.utils.R;
import org.diffshop.ware.feign.ProductFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.diffshop.common.utils.PageUtils;
import org.diffshop.common.utils.Query;

import org.diffshop.ware.dao.WmsWareSkuDao;
import org.diffshop.ware.entity.WmsWareSkuEntity;
import org.diffshop.ware.service.WmsWareSkuService;

@Slf4j
@Service("wmsWareSkuService")
public class WmsWareSkuServiceImpl extends ServiceImpl<WmsWareSkuDao, WmsWareSkuEntity> implements WmsWareSkuService {
    @Autowired
    private ProductFeignService productFeignService;
    /**
     * 分页查询带条件
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<WmsWareSkuEntity> queryWrapper = new LambdaQueryWrapper<>();

        String skuId = (String) params.get("skuId");
        if (StringUtils.isNotEmpty(skuId)){
            queryWrapper.eq(WmsWareSkuEntity::getSkuId, skuId);
        }

        String wareId = (String) params.get("wareId");
        if (StringUtils.isNotEmpty(wareId) && !"0".equals(wareId)){
            queryWrapper.eq(WmsWareSkuEntity::getWareId, wareId);
        }

        IPage<WmsWareSkuEntity> page = this.page(
                new Query<WmsWareSkuEntity>().getPage(params), queryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 添加库存
     * @param skuId
     * @param wareId
     * @param skuNum
     */
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        // 判断当前sku是否有库存
        LambdaQueryWrapper<WmsWareSkuEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmsWareSkuEntity::getSkuId, skuId).eq(WmsWareSkuEntity::getWareId, wareId);

        WmsWareSkuEntity skuEntity = this.getOne(queryWrapper);
        WmsWareSkuEntity entity = new WmsWareSkuEntity();
        if (skuEntity != null){ // 有库存
            entity.setStock(skuEntity.getStock() + skuNum);
        }else{// 无库存
            entity.setSkuId(skuId);
            entity.setWareId(wareId);
            entity.setStock(skuNum);
            // 远程调用product服务，查询skuName
            try {
                R result = productFeignService.querySkuInfo(skuId);
                Map<String, Object> data = (Map<String, Object>) result.get("pmsSkuInfo");
                entity.setSkuName((String) data.get("skuName"));
            } catch (Exception e) {
                log.error("添加库存时远程查询skuName失败，{}", e.getMessage());
            }
        }
        this.save(entity);
    }

}
